<%--
功能: 地区联动选择器
--%>
<%@ tag body-content="empty"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ tag import="org.springframework.context.ApplicationContext" %>
<%@ tag import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ tag import="com.probiz.estore.system.service.RegionManager" %>
<%@ tag import="com.probiz.estore.common.model.system.Region" %>
<%@ tag import="com.probiz.estore.Constants" %>
<%@ attribute name="selectorId" required="true" rtexprvalue="true" type="java.lang.String" description="selector id"%>
<%@ attribute name="regionId"  required="false" rtexprvalue="true" type="java.lang.Integer" description="当前选中的地区id"%>
<%@ attribute name="needValidate"  required="false" rtexprvalue="true" type="java.lang.Boolean" description="是否需要验证"%>

<% 
	if(null != regionId){
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
		RegionManager regionManager = (RegionManager) ctx.getBean("regionManager");
		Region targetRegion = regionManager.getById(regionId);
		if(null != targetRegion){
			request.setAttribute("targetRegion",targetRegion);
		}
	}
	request.setAttribute("rootRegionId",Constants.REGIONID_CHINA);
%>

<span id="${selectorId}">
	<span id="${selectorId}_loading"><fmt:message key="regionLinkageSelector.loading"/></span>
</span>	
<!-- 当前选中的地区id -->
<input type="hidden" id="regionId_${selectorId}" name="regionId" value=""/>

<c:set var="probizJs_dwr_util" value="true" scope="request"/>	
<c:set var="probizJs_regionLinkage" value="true" scope="request"/>	
<script type="text/javascript">
//要加载的array，里面放了regionId的一个序列值，用于初始化地区，例如要加载广东,广州,越秀。 则存放了越秀,广州的地区id(倒序)
var loadRegionArray_${selectorId} = new Array();
$j(document).ready(function(){
	__FMTExtend = {
		common_message_pleaseSelect:"<fmt:message key="common.message.pleaseSelect"/>"
	};
	$j.extend(__FMT, __FMTExtend);  
	<c:choose>
		<c:when test="${empty targetRegion}">
			initRegions_${selectorId}(${rootRegionId});
		</c:when>
		<c:otherwise>
			<c:forEach items="${targetRegion.parentsAndSelf}" var="region">
				loadRegionArray_${selectorId}.push(${region.regionId});
			</c:forEach>
			loadRegionArray_${selectorId}.shift();//去掉首个地区id
			initRegions_${selectorId}(${targetRegion.parentsAndSelf[0].regionId});
		</c:otherwise>
	</c:choose>
}); 

//选择地区
function selectRegion_${selectorId}(selectObj){
	var selectedIndex = selectObj.selectedIndex;
	$j(selectObj).nextAll('select').remove();
	$j(selectObj).nextAll('.error_txt').remove();
	if(selectedIndex!=0){
		$j('#regionId_${selectorId}').val(selectObj.options[selectedIndex].value);
		initRegions_${selectorId}(selectObj.options[selectedIndex].value);
	}else{
		//变为上一个select的已选择regionId
		var jPreSelect = $j(selectObj).prev('select');
		if(jPreSelect.length!=0){
			$j('#regionId_${selectorId}').val(jPreSelect.get(0).value);
		}else{
			$j('#regionId_${selectorId}').val("");
		}
	}
	
}

/**
*获取地区
*@param parentRegionId 父地区id
**/	
function initRegions_${selectorId}(parentRegionId){
	DWREngine.setAsync(false);
	regionDwr.getRegionsByParentId(parentRegionId,initRegionsCallback_${selectorId});
	DWREngine.setAsync(true);
}

/**
*获取地区callback
**/	
function initRegionsCallback_${selectorId}(regionList){
	var loadRegionId=-1;  //要加载的下个地区id
	var selectedIndex=-1;   //要设置为已选的option的index
	if(regionList.length>0){
		parentRegionId = regionList[0].parentRegionId;
		var html = '';
		html += '<select onchange="selectRegion_${selectorId}(this)"  id="${selectorId}_select_'+parentRegionId+'" name="regionName"><option value="">'+__FMT.common_message_pleaseSelect+'</option></select>';
		$j('#${selectorId}_loading').remove();
		$j('#${selectorId}').append(html);
		if(loadRegionArray_${selectorId}.length!=0){
			loadRegionId = loadRegionArray_${selectorId}.shift();
		}
		//加载所有options
		for(i=0;i<regionList.length;i++){
			DWRUtil.addOptions('${selectorId}_select_'+parentRegionId,[{regionId:regionList[i].regionId,regionName:regionList[i].regionName}],"regionId","regionName");
			if(loadRegionId == regionList[i].regionId){
				//获取当前要选择的select的selectedIndex
				selectedIndex = i+1;
			}
		}
		if(selectedIndex!=-1){
			//加载下一个地区
			$('${selectorId}_select_'+parentRegionId).selectedIndex=selectedIndex;
			selectRegion_${selectorId}($('${selectorId}_select_'+parentRegionId));
		}
		
		<c:if test="${needValidate}">
			$j('#${selectorId} select[name="regionName"]').each(function(){
				applyValidate(this,"required");
			});
		</c:if>
		
	}
}


</script>

